<style lang="less" scoped>
  .quality-indicators{
    width: 100%;
    height: 100%;
    &_title{
      height: 40px;
      background: url('~@/images/contenttitle.png');
      background-repeat: no-repeat;
      background-position: bottom;
      text-align: center;
      color: #00F6FF;
      line-height: 40px;
      margin-bottom: 5px;
    }
    &_content{
      height: calc(100% - 45px);
    }
  }
</style>

<template>
  <div class="quality-indicators">
    <div class="quality-indicators_title">
      <span>月度质控指标达标率</span>
    </div>
    <div id="qualityIndicators" class="quality-indicators_content" />
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { getQualityScreenRatio, getQualityScreenStrokeRatio, getQualityScreenTrauma } from '@/api/comprehensive/qualityIndicators'
export default {
  props: {
    isStrock: {
      type: String,
      default: 'chestPain'
    }
  },
  data() {
    return {
      setInter: null
    }
  },
  watch: {
    'isStrock'() {
      this.getQuality()
    }
  },
  mounted() {
    this.getQuality()
    this.setInter = setInterval(() => {
      this.getQuality()
    }, 5000)
  },
  destroyed() {
    clearInterval(this.setInter)
  },
  methods: {
    getQuality() {
      const myChart = echarts.init(document.getElementById('qualityIndicators'))
      // myChart.clear()
      myChart.showLoading({
        text: '',
        fontSize: 18,
        left: 'center',
        color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
        textColor: '#fff', // 文字颜色
        maskColor: 'rgba(255, 255, 255, 0)' // 背景色
      })
      window.addEventListener('resize', myChart.resize)
      let timeData = []
      let currentMonth = []
      let lastMonth = []
      let lastYears = []
      if (this.isStrock == 'chestPain') {
        getQualityScreenRatio({
          hospitalId: this.$store.getters.hospitalId
        }).then(res => {
          if (res.code == 0) {
            if (res.data && res.data.length > 0) {
              res.data.forEach(el => {
                timeData.push(el.timeName)
                currentMonth.push(el.ratioCurrentMonth ? el.ratioCurrentMonth : 0)
                lastMonth.push(el.ratioLastMonth ? el.ratioLastMonth : 0)
                lastYears.push(el.ratioLastYear ? el.ratioLastYear : 0)
              })
            }
            if (timeData.length > 0) {
              const option = {
                color: ['#59BCFB', '#01EEF6', '#00D59D'],
                legend: {
                  type: 'plain',
                  top: 0,
                  right: '15px',
                  itemWidth: 15,
                  itemHeight: 5,
                  icon: 'roundRect',
                  textStyle: {
                    color: '#fff'
                  },
                  formatter: function(name) {
                    return name
                  }
                },
                grid: {
                  top: '20%',
                  letf: '10%',
                  bottom: '20%'
                },
                xAxis: {
                  type: 'category',
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  },
                  axisLabel: {
                    color: 'fff',
                    interval: 0,
                    align: 'center',
                    textStyle: {
                      fontSize: 12
                    },
                    rotate: 10
                  },
                  data: timeData
                },
                yAxis: [{
                  name: '（百分比）',
                  type: 'value',
                  nameTextStyle: {
                    color: '#fff'
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  }
                }],
                tooltip: {
                  trigger: 'axis',
                  textStyle: {
                    color: '#00F6FF'
                  },
                  axisPointer: {
                    type: 'cross',
                    crossStyle: {
                      color: '#999'
                    }
                  },
                  formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}<br />{a2}: {c2}'
                },
                series: [
                  {
                    name: '本月',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: currentMonth,
                    type: 'bar'
                  },
                  {
                    name: '上月',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: lastMonth,
                    type: 'bar'
                  },
                  {
                    name: '去年',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: lastYears,
                    type: 'bar'
                  }
                ]
              }
              myChart.setOption(option)
            } else {
              myChart.showLoading({
                text: '暂无数据',
                fontSize: 18,
                left: 'center',
                color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
                textColor: '#fff', // 文字颜色
                maskColor: 'rgba(255, 255, 255, 0)' // 背景色
              })
            }
          }
        })
      } else if (this.isStrock == 'stroke') {
        getQualityScreenStrokeRatio({
          hospitalId: this.$store.getters.hospitalId
        }).then(res => {
          if (res.code == 0) {
            console.log(res.data)
            if (res.data && res.data.length > 0) {
              timeData = ['静脉溶栓率', 'DNT达标率', 'DPT达标率', '非心源性脑梗死的抗血小板治疗比率', '他汀类药物使用比率', '吞咽困难筛查率']
              res.data.forEach(el => {
                if (el.timeName == '本月') {
                  currentMonth = [el.ratioNeedle, el.reachRatioDNT, el.reachRatioDPT, el.ratioAntiplatelet, el.ratioStatins, el.ratioValuateDeglutition]
                }
                if (el.timeName == '上月') {
                  lastMonth = [el.ratioNeedle, el.reachRatioDNT, el.reachRatioDPT, el.ratioAntiplatelet, el.ratioStatins, el.ratioValuateDeglutition]
                }
                if (el.timeName == '上年') {
                  lastYears = [el.ratioNeedle, el.reachRatioDNT, el.reachRatioDPT, el.ratioAntiplatelet, el.ratioStatins, el.ratioValuateDeglutition]
                }
              })
            }
            if (timeData.length > 0) {
              const option = {
                color: ['#59BCFB', '#01EEF6', '#00D59D'],
                legend: {
                  type: 'plain',
                  top: 0,
                  right: '15px',
                  itemWidth: 15,
                  itemHeight: 5,
                  icon: 'roundRect',
                  textStyle: {
                    color: '#fff'
                  },
                  formatter: function(name) {
                    return name
                  }
                },
                grid: {
                  top: '20%',
                  letf: '10%',
                  bottom: '20%'
                },
                xAxis: {
                  type: 'category',
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  data: timeData
                },
                yAxis: [{
                  name: '（百分比）',
                  type: 'value',
                  nameTextStyle: {
                    color: '#fff'
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  }
                }],
                tooltip: {
                  trigger: 'axis',
                  textStyle: {
                    color: '#00F6FF'
                  },
                  axisPointer: {
                    type: 'cross',
                    crossStyle: {
                      color: '#999'
                    }
                  },
                  formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}<br />{a2}: {c2}'
                },
                series: [
                  {
                    name: '本月',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: currentMonth,
                    type: 'bar'
                  },
                  {
                    name: '上月',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: lastMonth,
                    type: 'bar'
                  },
                  {
                    name: '去年',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: lastYears,
                    type: 'bar'
                  }
                ]
              }
              myChart.setOption(option)
            } else {
              myChart.showLoading({
                text: '暂无数据',
                fontSize: 18,
                left: 'center',
                color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
                textColor: '#fff', // 文字颜色
                maskColor: 'rgba(255, 255, 255, 0)' // 背景色
              })
            }
          }
        })
      } else if (this.isStrock == 'trauma') {
        getQualityScreenTrauma({
          hospitalId: this.$store.getters.hospitalId
        }).then(res => {
          if (res.code == 0) {
            console.log(res.data)
            if (res.data && res.data.length > 0) {
              timeData = ['急诊准备时间', '完成全身快速CT', '完成全身胸部X片', '急诊输血准备时间', '人工气道建立时间', '紧急手术术前准备时间']
              res.data.forEach(el => {
                if (el.timeName == '本月') {
                  currentMonth = [el.avg_emerg_preparation_minutes, el.avg_complete_whole_fast_CT_minutes, el.avg_complete_whole_x_ray_minutes, el.avg_emergen_blood_preparation_minutes, el.avg_establish_artificial_airway_minutes, el.avg_emergency_surgery_preparation_minutes]
                }
                if (el.timeName == '上月') {
                  lastMonth = [el.avg_emerg_preparation_minutes, el.avg_complete_whole_fast_CT_minutes, el.avg_complete_whole_x_ray_minutes, el.avg_emergen_blood_preparation_minutes, el.avg_establish_artificial_airway_minutes, el.avg_emergency_surgery_preparation_minutes]
                }
                if (el.timeName == '上年') {
                  lastYears = [el.avg_emerg_preparation_minutes, el.avg_complete_whole_fast_CT_minutes, el.avg_complete_whole_x_ray_minutes, el.avg_emergen_blood_preparation_minutes, el.avg_establish_artificial_airway_minutes, el.avg_emergency_surgery_preparation_minutes]
                }
              })
            }
            if (timeData.length > 0) {
              const option = {
                color: ['#59BCFB', '#01EEF6', '#00D59D'],
                legend: {
                  type: 'plain',
                  top: 0,
                  right: '15px',
                  itemWidth: 15,
                  itemHeight: 5,
                  icon: 'roundRect',
                  textStyle: {
                    color: '#fff'
                  },
                  formatter: function(name) {
                    return name
                  }
                },
                grid: {
                  top: '20%',
                  letf: '10%',
                  bottom: '20%'
                },
                xAxis: {
                  type: 'category',
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  data: timeData
                },
                yAxis: [{
                  name: '（百分比）',
                  type: 'value',
                  nameTextStyle: {
                    color: '#fff'
                  },
                  axisLabel: {
                    color: 'fff'
                  },
                  axisLine: {
                    lineStyle: {
                      color: '#59BCFB33'
                    }
                  },
                  splitLine: {
                    show: false
                  },
                  axisTick: {
                    show: false
                  }
                }],
                tooltip: {
                  trigger: 'axis',
                  textStyle: {
                    color: '#00F6FF'
                  },
                  axisPointer: {
                    type: 'cross',
                    crossStyle: {
                      color: '#999'
                    }
                  },
                  formatter: '{b}<br />{a0}: {c0}<br />{a1}: {c1}<br />{a2}: {c2}'
                },
                series: [
                  {
                    name: '本月',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: currentMonth,
                    type: 'bar'
                  },
                  {
                    name: '上月',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: lastMonth,
                    type: 'bar'
                  },
                  {
                    name: '去年',
                    barWidth: 10,
                    barGap: '200%',
                    label: {
                      formatter: '{c}%',
                      show: true,
                      color: '#fff',
                      position: 'top'
                    },
                    data: lastYears,
                    type: 'bar'
                  }
                ]
              }
              myChart.setOption(option)
            } else {
              myChart.showLoading({
                text: '暂无数据',
                fontSize: 18,
                left: 'center',
                color: 'transparent', // loading颜色，设置成透明或白色，不然会显示loading状态
                textColor: '#fff', // 文字颜色
                maskColor: 'rgba(255, 255, 255, 0)' // 背景色
              })
            }
          }
        })
      }
    }
  }
}
</script>
